﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jy123.Web.AppCode;
using System.Collections.Generic;

namespace Jy123.Web.Manage
{
    public partial class Consume : MgConfig
    {
        private List<Jy123.Model.Public_JType> listJType;
        private Dictionary<int, string> JTypeDic = new Dictionary<int, string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Consume.aspx");
            string strWhere = "JFather = '1' OR JFather = '3'";
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            listJType = BLLJType.GetModelList(strWhere);
            foreach (Jy123.Model.Public_JType JType in listJType)
            {
                JTypeDic.Add(JType.ID, JType.JName);
            }
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Consume consume = e.Item.DataItem as Jy123.Model.Public_Consume;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = consume.ID.ToString();
                Label peakValueLabel = e.Item.FindControl("peakValueLabel") as Label;
                Label raceLabel = e.Item.FindControl("raceLabel") as Label;
                Label rentLabel = e.Item.FindControl("rentLabel") as Label;
                Label JTypeLabel = e.Item.FindControl("JTypeLabel") as Label;
                peakValueLabel.Text = consume.PeakValue.Value.ToString();
                raceLabel.Text = consume.RaceScore.Value.ToString();
                rentLabel.Text = consume.RentScore.Value.ToString();
                JTypeLabel.Text = JTypeDic[consume.FJType.Value];
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Consume BLLConsume = new Jy123.BLL.Public_Consume();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Consume consume = BLLConsume.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();                
                peakValueTextBox.Text = consume.PeakValue.Value.ToString();
                raceTextBox.Text = consume.RaceScore.Value.ToString();
                rentTextBox.Text = consume.RentScore.Value.ToString();                
                if (consume.FJType.HasValue)
                {
                    JTypeDropDown.SelectedValue = consume.FJType.Value.ToString();
                }
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLConsume.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Consume BLLConsume = new Jy123.BLL.Public_Consume();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Consume consume = new Jy123.Model.Public_Consume();
                SetValue(consume);
                BLLConsume.Add(consume);              
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Consume consume = BLLConsume.GetModel(id);
                SetValue(consume);
                BLLConsume.Update(consume);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            peakValueTextBox.Text = string.Empty;
            raceTextBox.Text = string.Empty;
            rentTextBox.Text = string.Empty; 
        }

        private void SetValue(Jy123.Model.Public_Consume consume)
        {
            consume.PeakValue = int.Parse(peakValueTextBox.Text);
            consume.RaceScore = int.Parse(raceTextBox.Text);
            consume.RentScore = int.Parse(rentTextBox.Text);
            consume.FJType = int.Parse(JTypeDropDown.SelectedValue);
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_Consume BLLConsume = new Jy123.BLL.Public_Consume();
            List<Jy123.Model.Public_Consume> listMaintain = BLLConsume.GetModelList(string.Empty);
            consumeRepeater.DataSource = listMaintain;
            consumeRepeater.DataBind();
            consumeRepeater.Visible = listMaintain.Count > 0;
            BindDropDown(listJType);
        }

        private void BindDropDown(List<Jy123.Model.Public_JType> listJType)
        {
            JTypeDropDown.Items.Clear();
            JTypeDropDown.DataSource = listJType;
            JTypeDropDown.DataTextField = "JName";
            JTypeDropDown.DataValueField = "ID";
            JTypeDropDown.DataBind();
            JTypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }
    }
}
